﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Tasks;
using Orchard;
using Ascent.XinKuanKong.Services;
using Orchard.Settings;
using Orchard.Logging;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;
using MongoDB.Driver;
using MongoDB.Bson;
using Top.Api.Domain;

namespace Ascent.XinKuanKong.Tasks
{
    public class TopTask : IBackgroundTask
    {
        private readonly IOrchardServices _orchardServices;
        private readonly IXinKuanKongService _xinkuankongService;
        private readonly ISiteService _siteService;
        protected ILogger Logger { get; set; }

        public TopTask(IOrchardServices orchardServices, IXinKuanKongService xinkuankongService, ISiteService siteService)
        {
            _orchardServices = orchardServices;
            _xinkuankongService = xinkuankongService;
            _siteService = siteService;
            Logger = NullLogger.Instance;
        }

        private MongoDatabase MongoDatabase { get { return _xinkuankongService.MongoDatabase; } }
        private DefaultTopClient DefaultTopClient{get { return _xinkuankongService.DefaultTopClient; }}
        private MongoCollection<Shop> ShopCollection { get { return MongoDatabase.GetCollection<Shop>("shops"); } }
        private MongoCollection<Item> ItemCollection { get { return MongoDatabase.GetCollection<Item>("items"); } }


        public void Sweep()
        {
            return;
            try
            {
                string nicks = "等待二月;upin365;蓝尾小鱼;非凡top主流;miumiu宝贝";
                foreach (var nick in nicks.Split(';'))
                {
                    ShopGetRequest shopGetRequest = new ShopGetRequest() { Fields = "sid,cid,title,nick,desc,bulletin,pic_path,created,modified,shop_score", Nick = nick };
                    ShopGetResponse shopGetResponse = DefaultTopClient.Execute<ShopGetResponse>(shopGetRequest);
                    if (shopGetResponse != null && shopGetResponse.Shop != null)
                    {
                        ShopCollection.Save(shopGetResponse.Shop);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
            }

            try
            {
                foreach (var shop in ShopCollection.FindAll())
                {
                    ItemsSearchRequest itemsSearchRequest = new ItemsSearchRequest() { Fields = "num_iid", Nicks = shop.Nick, PageSize = 200 };
                    ItemsSearchResponse itemsSearchResponse = DefaultTopClient.Execute<ItemsSearchResponse>(itemsSearchRequest);
                    if (itemsSearchResponse.ItemSearch != null)
                    {
                        foreach (var item in itemsSearchResponse.ItemSearch.Items)
                        {
                            ItemGetRequest itemGetRequest = new ItemGetRequest() { Fields = "detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual", NumIid = item.NumIid };
                            ItemGetResponse itemGetResponse = DefaultTopClient.Execute<ItemGetResponse>(itemGetRequest);
                            if (itemGetResponse.Item != null)
                            {
                                ItemCollection.Save(itemGetResponse.Item);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
            }

        }
    }
}